package com.xiyuan.smartutils.client.ws.core.auth;

import java.util.List;

import com.xiyuan.smartutils.client.exception.WebSocketException;
import com.xiyuan.smartutils.client.ws.util.Base64;



/**
 * The Class ProxyBasicCredentials.
 *
 * @author xiyuan-lgz
 */
public class BasicAuthenticator extends AbstractAuthenticator {

    /**
     * Instantiates a new proxy basic credentials.
     * this is not check the server auth realm
     *
     */
    public BasicAuthenticator(){
    }
        
    /* (non-Javadoc)
     * @see jp.a840.websocket.proxy.ProxyCredentials#getCredentials()
     */
    public String getCredentials(List<Challenge> challengeList) throws WebSocketException {
        for(Challenge challenge : challengeList){
            if (AuthScheme.Basic.equals(challenge.getScheme())) {
                return getCredentials(challenge);
            }
        }
        return null;
    }
    
    /**
     * Gets the credentials.
     *
     * @param challenge the challenge
     * @return the credentials
     * @throws WebSocketException the web socket exception
     */
    public String getCredentials(Challenge challenge) throws WebSocketException {
        String credentialsStr = this.credentials.getUsername() + ":" + this.credentials.getPassword();
        return AuthScheme.Basic.name() + " "
                + Base64.encodeToString(credentialsStr.getBytes(), false);
    }
}
